/// 聊天消息实体类
class ChatMessage {
  final String id;
  final String senderId;
  final String receiverId;
  final String content;
  final MessageType type;
  final DateTime createdAt;
  final bool isRead;

  ChatMessage({
    required this.id,
    required this.senderId,
    required this.receiverId,
    required this.content,
    required this.type,
    required this.createdAt,
    this.isRead = false,
  });

  factory ChatMessage.fromJson(Map<String, dynamic> json) {
    return ChatMessage(
      id: json['id'],
      senderId: json['senderId'],
      receiverId: json['receiverId'],
      content: json['content'],
      type: MessageType.values[json['type']],
      createdAt: DateTime.parse(json['createdAt']),
      isRead: json['isRead'] ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'senderId': senderId,
      'receiverId': receiverId,
      'content': content,
      'type': type.index,
      'createdAt': createdAt.toIso8601String(),
      'isRead': isRead,
    };
  }

  ChatMessage copyWith({
    String? id,
    String? senderId,
    String? receiverId,
    String? content,
    MessageType? type,
    DateTime? createdAt,
    bool? isRead,
  }) {
    return ChatMessage(
      id: id ?? this.id,
      senderId: senderId ?? this.senderId,
      receiverId: receiverId ?? this.receiverId,
      content: content ?? this.content,
      type: type ?? this.type,
      createdAt: createdAt ?? this.createdAt,
      isRead: isRead ?? this.isRead,
    );
  }
}

/// 消息类型枚举
enum MessageType {
  text,    // 文本消息
  image,   // 图片消息
  emoji,   // 表情消息
}

/// 聊天会话实体类
class ChatConversation {
  final String id;
  final String userId1;
  final String userId2;
  final String? lastMessageId;
  final DateTime lastActiveAt;
  final int unreadCount;

  ChatConversation({
    required this.id,
    required this.userId1,
    required this.userId2,
    this.lastMessageId,
    required this.lastActiveAt,
    this.unreadCount = 0,
  });

  factory ChatConversation.fromJson(Map<String, dynamic> json) {
    return ChatConversation(
      id: json['id'],
      userId1: json['userId1'],
      userId2: json['userId2'],
      lastMessageId: json['lastMessageId'],
      lastActiveAt: DateTime.parse(json['lastActiveAt']),
      unreadCount: json['unreadCount'] ?? 0,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'userId1': userId1,
      'userId2': userId2,
      'lastMessageId': lastMessageId,
      'lastActiveAt': lastActiveAt.toIso8601String(),
      'unreadCount': unreadCount,
    };
  }

  ChatConversation copyWith({
    String? id,
    String? userId1,
    String? userId2,
    String? lastMessageId,
    DateTime? lastActiveAt,
    int? unreadCount,
  }) {
    return ChatConversation(
      id: id ?? this.id,
      userId1: userId1 ?? this.userId1,
      userId2: userId2 ?? this.userId2,
      lastMessageId: lastMessageId ?? this.lastMessageId,
      lastActiveAt: lastActiveAt ?? this.lastActiveAt,
      unreadCount: unreadCount ?? this.unreadCount,
    );
  }

  /// 获取对方用户ID
  String getOtherUserId(String currentUserId) {
    return userId1 == currentUserId ? userId2 : userId1;
  }

  /// 检查是否包含指定用户
  bool containsUser(String userId) {
    return userId1 == userId || userId2 == userId;
  }
}
